{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#该函数主要以用来画图。用meshgrid生成坐标矩阵，对坐标矩阵中的每个点应用模型来做预测，根据预测的结果来绘制等高线图\n",
    "def plot_hyperplane(clf, X, y, \n",
    "                    h=0.02, \n",
    "                    draw_sv=True, \n",
    "                    title='hyperplan'):\n",
    "  \n",
    "    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1     \n",
    "    \n",
    "    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1     \n",
    "    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),\n",
    "                         np.arange(y_min, y_max, h))    #生成坐标矩阵     \n",
    "\n",
    "    plt.title(title)\n",
    "    plt.xlim(xx.min(), xx.max())  #设定x轴的取值范围\n",
    "    plt.ylim(yy.min(), yy.max())\n",
    "    plt.xticks(())  #清除x轴的刻度\n",
    "    plt.yticks(())\n",
    "    \n",
    "    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])   #np.c_ 是按行链接两个矩阵，前提是两个矩阵的行数相同\n",
    "   \n",
    "    Z = Z.reshape(xx.shape)  #把Z变换成与xx一样的形状，画图参数的要求\n",
    "    plt.contourf(xx, yy, Z, cmap='hot', alpha=0.5)   #contourf画带填充的等高线图\n",
    "    \n",
    "    #用来画样本数据\n",
    "\n",
    "    markers = ['o', 's', '^']  #样本数据的不同标记\n",
    "    colors = ['b', 'r', 'c']  #不同颜色\n",
    "    labels = np.unique(y)  #y取值\n",
    "    \n",
    "    #根据y取值的不同，选择不同的颜色不同的标记来画点\n",
    "    for label in labels:\n",
    "        plt.scatter(X[y==label][:, 0], \n",
    "                    X[y==label][:, 1], \n",
    "                    c=colors[label], \n",
    "                    marker=markers[label])\n",
    "        \n",
    "    if draw_sv:  #要画出支持向量\n",
    "        sv = clf.support_vectors_    #  模型的属性support_vectors_中保存了所有支持向量\n",
    "        plt.scatter(sv[:, 0], sv[:, 1], c='y', marker='x')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.svm import SVC\n",
    "from sklearn.datasets import make_blobs\n",
    "\n",
    "X,y=make_blobs(n_samples=100, centers=2,random_state=0,cluster_std=0.3)  #创建样本，100个样本，两个类别，类别有centers决定\n",
    "\n",
    "model=SVC(C=10.0,kernel='linear')  #创建线性SVM模型\n",
    "model.fit(X,y)\n",
    "\n",
    "plt.figure(figsize=(12,8))\n",
    "plot_hyperplane(model,X,y,h=0.01,title='Maximum Margin Hyperplan')  #调用上面的等高线函数来画结果图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#重新生成一批样本数据，并生成四个不同的SVM模型，分别训练，测试他们，比较他们的结果，在一张图的四个子图中画出来\n",
    "X,y=make_blobs(n_samples=100, centers=3,random_state=0,cluster_std=0.8)\n",
    "\n",
    "model_linear=SVC(C=1.0,kernel='linear')\n",
    "\n",
    "model_poly=SVC(C=1.0,kernel='poly',degree=3)  #三阶多项式核函数\n",
    "\n",
    "model_rbf=SVC(C=1.0,kernel='rbf',gamma=0.5)\n",
    "\n",
    "model_rbf2=SVC(C=1.0,kernel='rbf',gamma=0.1)\n",
    "\n",
    "models=[model_linear,model_poly,model_rbf,model_rbf2]\n",
    "\n",
    "titles=['Linear Kernel','Polynomial Kernel with Degree=3','Gaussian Kernel with gamma=0.5','Gaussian Kernel with gamma=0.1']\n",
    "\n",
    "plt.figure(figsize=(16,16))\n",
    "\n",
    "for model,i in zip(models,range(len(models))):\n",
    "    model.fit(X,y)\n",
    "    plt.subplot(2,2,i+1)\n",
    "    plot_hyperplane(model,X,y,title=titles[i])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
